#ifndef CAMERA_VIDEO_SINK_H
#define CAMERA_VIDEO_SINK_H

// vcm_capturer_test.h
#include <memory>
#include <QObject>
#include "modules/video_capture/video_capture.h"
#include "video_source_impl.h"


class FrameBufferList{
public:
    uint8_t* TakeLast();
    void PushFirst(uint8_t*);
    uint16_t Size();
private:
    std::list<uint8_t*> m_data;
    std::mutex m_mutex;
};

class CameraVideoSink : public QObject,
                        public VideoSource,
                        public rtc::VideoSinkInterface<webrtc::VideoFrame>
{
    Q_OBJECT
public:
    CameraVideoSink();
    static CameraVideoSink* Create(size_t width,
                                 size_t height,
                                 size_t target_fps,
                                 size_t capture_device_index);
    virtual ~CameraVideoSink();
    void OnFrame(const webrtc::VideoFrame& frame) override;
    webrtc::VideoCaptureCapability Capability();
    FrameBufferList *VideoBuffer(){
        return &this->m_buf;
    }
signals:
    void UpdateFrame(rtc::scoped_refptr<webrtc::I420BufferInterface>& buffer);
private:
    bool Init(size_t width,
        size_t height,
        size_t target_fps,
        size_t capture_device_index);
    void Destroy();

    rtc::scoped_refptr<webrtc::VideoCaptureModule> m_vcm;
    webrtc::VideoCaptureCapability m_capability;
    FrameBufferList m_buf;
};

#endif // CAMERA_VIDEO_SINK_H


